package org.meteorshower.system_depart.controller;

import org.meteorshower.cms_common.exception.*;
import org.meteorshower.cms_common.result.ResponseObj;
import org.meteorshower.system_depart.entity.Role;
import org.meteorshower.system_depart.service.RoleServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleServiceInter roleService;


    @GetMapping("/{rid}")
    public ResponseObj<Role> getRoleById(@PathVariable("rid") Integer id) throws ResultNullPointException, ServiceException, ParameterNullPointException {
        return ResponseObj.ok(roleService.getById(id));
    }

    @GetMapping("/all")
    public ResponseObj<List<Role>> getRoleAll() throws ServiceException, ResultNullPointException {
        return ResponseObj.ok(roleService.getAll());
    }

    @GetMapping("/name/{name}")
    public ResponseObj<Role> getRoleByName(@PathVariable("name") String name) throws ResultNullPointException, ServiceException, ParameterNullPointException {
        return ResponseObj.ok(roleService.getByName(name));
    }

    @PostMapping
    public ResponseObj<Integer> insertRole(@RequestBody Role role) throws ServiceException, ParameterNullPointException, RecordExitException {
        return ResponseObj.ok(roleService.insert(role));
    }

    @PutMapping
    public ResponseObj<Integer> updateRole(@RequestBody Role role) throws UnknownRecordException, ServiceException, ParameterNullPointException {
        return ResponseObj.ok(roleService.update(role));
    }

    @DeleteMapping("/{rid}")
    public ResponseObj<Integer> deleteRole(@PathVariable("rid") Integer id) throws ServiceException, ParameterNullPointException, UnknownRecordException {
        return ResponseObj.ok(roleService.deleteById(id));
    }

}
